﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using StoragePeriod.Web.UserControls;
using System.Web.Security;
using StoragePeriod.Data;
using System.Web.UI.WebControls;

namespace StoragePeriod.Common
{
    public class SecurityPage : Page
    {

        /// <summary>
        /// 左侧树控件的加载事件
        /// </summary>
        public virtual void TreeSelectChange()
        {

        }

        public SYS_Users CurrentUser { get; set; }

        public virtual string PrivCode()
        {
            // throw new Exception("页面未受控");
            return "";
        }

        public string PrivilegeCode { get { return PrivCode(); } }
        public List<string> userAllRolePriv { get; set; }


        public override void Dispose()
        {
            base.Dispose(); 

            LinqUtility.LinqContext.Dispose();
            LinqUtility.LinqContextCMS.Dispose();
        }

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            if (this.DesignMode) return;
            LinqUtility.LinqContext = new StoragePeriodDataContext();
            LinqUtility.LinqContextCMS = new CMSCateringDataContext();

            if (User.Identity.IsAuthenticated)
            {
                CurrentUser = LinqUtility.LinqContext.SYS_Users.FirstOrDefault(p => p.UserCode == User.Identity.Name);


                
            }


            userAllRolePriv = (from a in LinqUtility.LinqContext.RoleUsers
                               join b in LinqUtility.LinqContext.RolePrivileges
                                   on a.RoleCode equals b.RoleCode
                               where a.UserCode == User.Identity.Name
                               select b.PrivCode).ToList();
            userAllRolePriv.Add("Default");
            userAllRolePriv.Add(""); //没有加的默认为有权限访问
            CheckEnablePage(userAllRolePriv);
        }

        //判断是否存在权限
        public bool IsExisPriv(string PrivCode)
        {
            bool b = true;
            b = userAllRolePriv.Contains(PrivCode);
            return b;
        }

        protected override void OnPreRender(EventArgs e)
        {
            base.OnPreRender(e);

            Page.Controls.AllOfType<Label>().ToList().ForEach(p =>
            {
                if (p.ID == "master_UserName")
                {
                    p.Text = CurrentUser.UserName;
                }
            });
        }

        protected override void Render(HtmlTextWriter writer)
        {
            
            base.Render(writer);
            if (PrivilegeCode.IsNullOrEmpty()) return;

            CheckEnableControl(userAllRolePriv);
        }

        private void CheckEnablePage(List<string> userAllRolePriv)
        {
            bool hasPrivilege = userAllRolePriv.Contains(PrivilegeCode);
            if (!hasPrivilege && User.Identity.IsAuthenticated)
            {
                Response.Redirect("~/NoPower.aspx");
                Page.AlertAndClose("您没有使用此页面的权限！请联系管理员。");
            }
        }

        private void CheckEnableControl(List<string> rolePrivs)
        {
            this.Controls.AllOfType<Button>().ToList().ForEach(p =>
              {
                  string PrivCode = p.Attributes["PrivCode"];
                  if (!PrivCode.IsNullOrEmpty())
                  {
                      p.Enabled = rolePrivs.Contains(PrivCode);
                  }
              });

            this.Controls.AllOfType<ImageButton>().ToList().ForEach(p =>
            {
                string PrivCode = p.Attributes["PrivCode"];
                if (!PrivCode.IsNullOrEmpty())
                {

                    p.Enabled = rolePrivs.Contains(PrivCode);
                }
            });

        }

        public MaterialTree LeftTree
        {
            get
            {
                return Page.Controls.AllOfType<MaterialTree>().FirstOrDefault();
            }
        }

        public Label LeftLable
        {
            get
            {
                return Page.Controls.AllOfType<Label>().Where(p => p.ID == "Left_Title").FirstOrDefault();
            }
        }

    }
}